﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq.Expressions;

namespace Common.Net.Gui
{
    public static class BindingHelper
    {
        public static void AddBinding<TControl, TDataSource>
        (
            this TControl control,
            Expression<Func<TControl, object>> property,
            TDataSource dataSource,
            Expression<Func<TDataSource, object>> dataMember
        )
            where TControl : Control
        {
            string propertyName = ((MemberExpression)property.Body).Member.Name;
            string dataMemberName = ((MemberExpression)dataMember.Body).Member.Name;
            var binding = new Binding(propertyName, dataSource, dataMemberName);
            control.DataBindings.Add(binding);
        }
    }
}
